package tsdb;

/**
 * @author caihe
 * @date 2021/12/29 9:57
 */

import com.alibaba.druid.pool.DruidDataSource;

import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class JDBCDemo {
    private DruidDataSource dataSource;

    public void setUp() throws SQLException {

        String url = "jdbc:lindorm:tsdb:url=http://ld-uf65nx39go0j575xu-proxy-tsdb-pub.lindorm.rds.aliyuncs.com:8242";
        Connection conn = null;
//
//    
//        conn = DriverManager.getConnection(url);
//        Statement stmt = conn.createStatement();
//            

        dataSource = new DruidDataSource();
        try {
            dataSource.setDriver(DriverManager.getDriver("jdbc:lindorm:tsdb:url=http://ld-uf65nx39go0j575xu-proxy-tsdb-pub.lindorm.rds.aliyuncs.com:8242"));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        dataSource.setUrl("jdbc:lindorm:tsdb:url=http://ld-uf65nx39go0j575xu-proxy-tsdb-pub.lindorm.rds.aliyuncs.com:8242");
        dataSource.setValidationQuery("select 1");
        dataSource.setMaxActive(8);
        dataSource.setUsername("root");
        dataSource.setPassword("root");
        dataSource.setKeepAlive(true);
        dataSource.setMinEvictableIdleTimeMillis(80);
        dataSource.setMaxWait(10000);
        dataSource.init();

    }

    public void close() {
        dataSource.close();
    }

    public void testQuery() {
        try (Connection conn = this.dataSource.getConnection()) {
            try (Statement stmt = conn.createStatement()) {
                ResultSet rs = stmt.executeQuery("select * from sensor");
                long count = 0;
                while (rs.next()) {
                    count++;
                }
                System.out.println("count=" + count + " ");
            } catch (RuntimeException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } catch (RuntimeException e) {
            System.out.println("connection failed.");
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
//        JDBCDemo app = new JDBCDemo();
//        app.setUp();
//        app.testQuery();
//        app.close();
        String time  = "2021-09-01 12:00:01";
        String time1 = "2021-08-01 01:00:04";
        String time2 = "2021-08-01 01:00:07";
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time));
        System.out.println(calendar.getTimeInMillis());
        calendar.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time1));
        System.out.println(calendar.getTimeInMillis());
        calendar.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time2));
        System.out.println(calendar.getTimeInMillis());
//        System.out.println(System.currentTimeMillis());

//1641284521000
    }
}